package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.requestVo.YiyaVisitDoctorRequest;
import com.yiyahui.project.erp.domain.responseVo.VisitDoctorExtend;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.project.erp.domain.YiyaVisitDoctor;
import com.yiyahui.project.erp.service.IYiyaVisitDoctorService;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.domain.BaseEntity;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 医生回访Controller
 * 
 * @author yiyahui
 * @date 2020-05-08
 */
@Api("医生回访")
@RestController
@RequestMapping("/erp/doctor")
public class YiyaVisitDoctorController extends BaseController
{
    @Autowired
    private IYiyaVisitDoctorService yiyaVisitDoctorService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;
 
    /**
     * 查询医生回访列表
     */
    @ApiOperation("查询医生回访列表")
    @PreAuthorize("@ss.hasPermi('erp:doctor:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaVisitDoctorRequest yiyaVisitDoctorRequest)
    {
        startPage();
        List<YiyaVisitDoctor> list = yiyaVisitDoctorService.selectYiyaVisitDoctorList(yiyaVisitDoctorRequest);
        return getDataTable(list);
    }

    /**
     * 导出医生回访列表
     */
    @ApiOperation("导出医生回访列表")
    @PreAuthorize("@ss.hasPermi('erp:doctor:export')")
    @Log(title = "医生回访", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaVisitDoctorRequest yiyaVisitDoctorRequest)
    {
        List<YiyaVisitDoctor> list = yiyaVisitDoctorService.selectYiyaVisitDoctorList(yiyaVisitDoctorRequest);
        ExcelUtil<YiyaVisitDoctor> util = new ExcelUtil<YiyaVisitDoctor>(YiyaVisitDoctor.class);
        return util.exportExcel(list, "doctor");
    }

    /**
     * 获取医生回访详细信息
     */
    @ApiOperation("获取医生回访详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:doctor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaVisitDoctorService.selectYiyaVisitDoctorById(id));
    }

    /**
     * 新增医生回访
     */
    @ApiOperation("新增医生回访")
    @ApiImplicitParam(name = "yiyaVisitDoctor", value = "${tableComment}", dataType = "YiyaVisitDoctor")
    @PreAuthorize("@ss.hasPermi('erp:doctor:add')")
    @Log(title = "医生回访", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaVisitDoctor yiyaVisitDoctor)
    {
        return toAjax(yiyaVisitDoctorService.insertYiyaVisitDoctor(yiyaVisitDoctor));
    }

    /**
     * 修改医生回访
     */
    @ApiOperation("修改医生回访")
    @ApiImplicitParam(name = "yiyaVisitDoctor", value = "${tableComment}", dataType = "YiyaVisitDoctor")
    @PreAuthorize("@ss.hasPermi('erp:doctor:edit')")
    @Log(title = "医生回访", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaVisitDoctor yiyaVisitDoctor)
    {
        return toAjax(yiyaVisitDoctorService.updateYiyaVisitDoctor(yiyaVisitDoctor));
    }

    /**
     * 删除医生回访
     */
    @ApiOperation("删除医生回访")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:doctor:remove')")
    @Log(title = "医生回访", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaVisitDoctorService.deleteYiyaVisitDoctorByIds(ids));
    }

    /**
     * 自定义医生回访列表
     */
    @ApiOperation("自定义医生回访列表")
    @PostMapping("/extend/list")
    public TableDataInfo extendList(@RequestBody YiyaVisitDoctorRequest yiyaVisitDoctorRequest)
    {
        startPageByPost(yiyaVisitDoctorRequest);
        List<YiyaVisitDoctor> list = yiyaVisitDoctorService.selectYiyaVisitDoctorList(yiyaVisitDoctorRequest);
        Map<Long, YiyaClinicPerson> yiyaClinicPersonMap = yiyaClinicPersonService.selectByIds(list.stream().map(YiyaVisitDoctor::getClinicPersonId).collect(Collectors.toList())).stream().collect(Collectors.toMap(YiyaClinicPerson::getId, a -> a, (k1, k2) -> k1));
        List<VisitDoctorExtend> visitTcExtends = new ArrayList<>();
        list.forEach(r -> {
            if (r.getClinicPersonId() != null && r.getClinicPersonId() > 0) {
                YiyaClinicPerson yiyaClinicPerson = yiyaClinicPersonMap.get(r.getClinicPersonId());
                if (yiyaClinicPerson != null) {
                    VisitDoctorExtend visitDoctorExtend = new VisitDoctorExtend();
                    visitDoctorExtend.setYiyaVisitDoctor(r);
                    visitDoctorExtend.setYiyaClinicPerson(yiyaClinicPerson);
                    visitTcExtends.add(visitDoctorExtend);
                }
            }
        });
        return getDataTable(visitTcExtends,list);
    }
}
